#ifndef _GS_DB_H_
#define _GS_DB_H_
/*******************************************************************************
 ** Name: db.h                                                                **
 ** Description:                                                              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved 2-clause BSD License                **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
// Modified $Date: 2010-03-16 20:45:00 -0600 (Tue, 16 Mar 2010) $ by $Author: bjames $
#define db_h_ID    "$Id: db.h 130 2010-03-17 02:45:00Z bjames $"

#include <iostream>
#include <fstream>
#include <map>
#include <vector>

#include <mysql++/mysql++.h>
#define MYSQLPP_SSQLS_NO_STATICS
#include "MySQLStructs.hpp"

#include "common.hpp"
#include "container.hpp"
//#include "MySQLArea.hpp"

#define DEFAULT_SERVER_CONF "sanctiond.conf"
#define HELP_SETTING    1
#define AGENCY_SETTING  2


//! The base DataBase functionality
class DataBase
{
public:
    //constructor and destructor
    DataBase(std::string);
    virtual ~DataBase(void);

    virtual bool        Init() = 0;
    std::string DB() const { return db; }
    void        SetDB(std::string d) { db = d; }

protected:
    std::string db;
    bool        bInitialized;
    
};

class MySQLDB : public DataBase
{
public:
    MySQLDB(std::string);

    bool        Init();
    mysqlpp::StoreQueryResult  Query(const char *);


    std::string Host() const { return host; }
    std::string User() const { return dbuser; }
    std::string Pass() const { return dbpass; }
    void        SetHost(const std::string h) { host = h; }
    void        SetUser(const std::string u) { dbuser = u; }
    void        SetPass(const std::string p) { dbpass = p; }

protected:
    std::string host;
    std::string dbuser;
    std::string dbpass;
    mysqlpp::Connection conn;

};

//! DataBase Reference
//! Designed to be passed around commonly and opaquely index
//! into the MySQL DB
class DBRef : public MySQLDB
{
public:
	DBRef(std::string q,std::string p);
	mysqlpp::StoreQueryResult ExecuteQuery();

private:
	std::string query;
};

//! The Area Database
class AreaDB : public MySQLDB
{
public:
	AreaDB(std::string f);

    std::vector<MySQLArea>         LoadAreas(int);
    int         LoadNPCs();

private:
    static const char *     qSettings;
    static const char *     qAreas;
    static const char *     qNPCs;
};

//! This is a very small configuration database that is used
//! to keep from passing passwords to the server on the command line
class ServerCfg : public DataBase
{
public:
    ServerCfg(std::string f = DEFAULT_SERVER_CONF);

    bool        Init();
    int         Parse();

    std::string GetValue(std::string a) { return avpairs[a]; }
    void        AddAVPair(std::string a, std::string v) { avpairs[a] = v; }
    
private:
    std::map<std::string,std::string>    avpairs;
    std::ifstream    conf;

};


#endif	/* _GS_DB_H_ */
